// Action Camera System Architecture - Main Header File

#ifndef ACTION_CAMERA_H
#define ACTION_CAMERA_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

// 系统配置常量
#define MAX_THREADS           8
#define MAX_FRAME_SIZE        (1920 * 1080 * 3)  // RGB24最大帧大小
#define FRAME_POOL_SIZE       16                  // 帧缓冲池大小
#define MESSAGE_QUEUE_SIZE    64                  // 消息队列大小
#define THREAD_TIMEOUT        5000000             // 线程超时时间(微秒)
#define MAX_FILENAME_LEN      256
#define MAX_MESSAGE_SIZE      1024

// 线程ID定义
typedef enum {
    THREAD_MAIN = 0,
    THREAD_CAMERA,
    THREAD_ENCODER,
    THREAD_AUDIO,
    THREAD_STORAGE,
    THREAD_UI,
    THREAD_MONITOR,
    THREAD_NETWORK
} thread_id_t;

// 消息类型定义
typedef enum {
    MSG_CONTROL,        // 控制消息
    MSG_VIDEO_FRAME,    // 视频帧数据
    MSG_AUDIO_FRAME,    // 音频帧数据
    MSG_USER_INPUT,     // 用户输入
    MSG_SYSTEM_EVENT,   // 系统事件
    MSG_ERROR,          // 错误消息
    MSG_SHUTDOWN        // 关机消息
} message_type_t;

// 系统状态定义
typedef enum {
    SYSTEM_INIT,
    SYSTEM_IDLE,
    SYSTEM_RECORDING,
    SYSTEM_PLAYBACK,
    SYSTEM_STREAMING,
    SYSTEM_ERROR,
    SYSTEM_SHUTDOWN
} system_state_t;

// 线程状态定义
typedef enum {
    THREAD_INIT,
    THREAD_RUNNING,
    THREAD_PAUSED,
    THREAD_ERROR,
    THREAD_TERMINATED
} thread_status_t;

// 视频格式定义
typedef enum {
    VIDEO_FORMAT_720P_30FPS,
    VIDEO_FORMAT_720P_60FPS,
    VIDEO_FORMAT_1080P_30FPS,
    VIDEO_FORMAT_1080P_60FPS,
    VIDEO_FORMAT_4K_30FPS
} video_format_t;

// 消息结构
typedef struct {
    message_type_t type;
    uint32_t timestamp;
    thread_id_t source_thread;
    thread_id_t dest_thread;
    uint32_t data_size;
    uint8_t data[MAX_MESSAGE_SIZE];
} message_t;

// 消息队列结构
typedef struct {
    message_t* messages;
    uint32_t head;
    uint32_t tail;
    uint32_t capacity;
    uint32_t count;
    pthread_mutex_t mutex;
    pthread_cond_t not_empty;
    pthread_cond_t not_full;
} message_queue_t;

// 共享帧缓冲区
typedef struct {
    uint32_t frame_id;
    uint64_t timestamp;
    uint32_t width;
    uint32_t height;
    uint32_t format;
    uint32_t size;
    atomic_int ref_count;
    bool is_keyframe;
    uint8_t data[MAX_FRAME_SIZE];
} shared_frame_t;

// 帧缓冲池
typedef struct {
    shared_frame_t* frames;
    uint32_t pool_size;
    atomic_int free_count;
    uint32_t next_free;
    pthread_mutex_t pool_mutex;
    pthread_cond_t frame_available;
} frame_pool_t;

// 线程监控结构
typedef struct {
    pthread_t thread_id;
    thread_status_t status;
    uint64_t last_heartbeat;
    uint32_t error_count;
    char thread_name[32];
    bool enabled;
} thread_monitor_t;

// 系统配置结构
typedef struct {
    video_format_t video_format;
    uint32_t video_bitrate;
    uint32_t audio_sample_rate;
    bool audio_enabled;
    bool wifi_enabled;
    bool auto_record;
    uint32_t max_file_size_mb;
    char storage_path[MAX_FILENAME_LEN];
} system_config_t;

// 系统统计信息
typedef struct {
    uint64_t frames_captured;
    uint64_t frames_encoded;
    uint64_t frames_dropped;
    uint64_t bytes_written;
    uint32_t cpu_usage;
    uint32_t memory_usage;
    uint32_t temperature;
    uint32_t battery_level;
} system_stats_t;

// 全局同步对象
typedef struct {
    pthread_mutex_t config_mutex;
    pthread_mutex_t stats_mutex;
    pthread_mutex_t log_mutex;
    
    pthread_cond_t shutdown_cond;
    
    atomic_bool system_running;
    atomic_int active_threads;
    _Atomic uint64_t system_timestamp;
} sync_objects_t;

// 主系统结构
typedef struct {
    system_state_t state;
    system_config_t config;
    system_stats_t stats;
    
    // 线程管理
    thread_monitor_t threads[MAX_THREADS];
    
    // 通信机制
    message_queue_t message_queues[MAX_THREADS];
    frame_pool_t frame_pool;
    
    // 同步对象
    sync_objects_t sync;
    
    // 运行时状态
    uint64_t start_time;
    char current_filename[MAX_FILENAME_LEN];
    
} action_camera_system_t;

// 全局系统实例
extern action_camera_system_t g_camera_system;

// 函数声明 - 系统管理
int system_init(void);
int system_start(void);
void system_shutdown(void);
void system_cleanup(void);

// 函数声明 - 线程管理
int create_all_threads(void);
void* camera_capture_thread(void* arg);
void* video_encoder_thread(void* arg);
void* audio_capture_thread(void* arg);
void* storage_manager_thread(void* arg);
void* ui_manager_thread(void* arg);
void* system_monitor_thread(void* arg);
void* network_service_thread(void* arg);

// 函数声明 - 消息传递
int message_queue_init(message_queue_t* queue, uint32_t capacity);
int message_queue_send(message_queue_t* queue, const message_t* msg);
int message_queue_receive(message_queue_t* queue, message_t* msg, uint32_t timeout_ms);
void message_queue_destroy(message_queue_t* queue);

// 函数声明 - 帧缓冲池
int frame_pool_init(frame_pool_t* pool, uint32_t pool_size);
shared_frame_t* frame_pool_acquire(frame_pool_t* pool);
void frame_pool_release(frame_pool_t* pool, shared_frame_t* frame);
void frame_pool_destroy(frame_pool_t* pool);

// 函数声明 - 工具函数
uint64_t get_system_timestamp(void);
void update_thread_heartbeat(thread_id_t thread_id);
void log_message(const char* level, const char* format, ...);
int set_thread_priority(pthread_t thread, int priority);
int set_thread_affinity(pthread_t thread, int cpu_id);

// Macro definitions - 使用GNU C扩展处理可变参数宏中无参数的情况
#define LOG_ERROR(fmt, ...)   log_message("ERROR", fmt, ##__VA_ARGS__)
#define LOG_WARN(fmt, ...)    log_message("WARN",  fmt, ##__VA_ARGS__)
#define LOG_INFO(fmt, ...)    log_message("INFO",  fmt, ##__VA_ARGS__)
#define LOG_DEBUG(fmt, ...)   log_message("DEBUG", fmt, ##__VA_ARGS__)

#define HEARTBEAT() update_thread_heartbeat(pthread_self())

#define SAFE_FREE(ptr) do { \
    if (ptr) { \
        free(ptr); \
        ptr = NULL; \
    } \
} while(0)

#endif // ACTION_CAMERA_H